package com.perfect.mybatis.spring;

import com.perfect.mapper.UserMapper;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import static java.lang.reflect.Proxy.newProxyInstance;

/**
 * @author chenshiping
 * @date 2025/3/24
 * @description TODO
 */
public class PerfectMapperFactoryBean  implements FactoryBean {
        private Class<?> mapperInterface;

        public PerfectMapperFactoryBean(Class<?> mapperInterface) {
                this.mapperInterface = mapperInterface;
        }

        @Override
        public Object getObject() throws Exception {

                Object o =  newProxyInstance(this.getClass().getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                System.out.println("proxy:"+proxy.getClass().getName());
                                System.out.println("method:"+method.getName());
                                return null;
                        }
                });
                return o;
        }

        @Override
        public Class<?> getObjectType() {
                return mapperInterface;
        }
}
